home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Sample 2.4 Think C distribution / wrap.c < prev    next >
C/C++ Source or Header  |  1990-07-13  |  3KB  |  129 lines

  1. /*______________________________________________________________________
  2.  
  3.     wrap.c - Tool to word wrap paragraphs.
  4.     
  5.     Author: John Norstad
  6.     
  7.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is granted
  8.     to use this code in your own projects, provided you give credit to both
  9.     John Norstad and Northwestern University in your about box or document.
  10.     
  11.     This tool reads standard input and writes standard output.  It
  12.     word wraps each paragraph from the input file.
  13.     
  14.     In Disinfectant I use this tool to help build the online document.  I
  15.     maintain the source using Microsoft word on a text-only file, without line 
  16.     breaks.  This saves each paragraph as one big long line.  My makefile 
  17.     runs the wrap tool to word wrap each of these paragraphs to the exact 
  18.     width of my help window rectangle.  The output of wrap is then fed through 
  19.     the cvrt tool to generate the sequence of STR# resources used by the program.
  20.     
  21.     wrap -r xxxx [-p]
  22.     
  23.     xxxx = right margin in pixels.
  24.     
  25.     -p = if specified, add a special end-of-paragraph byte 31 to the
  26.         end of each line.  This option should be specified when preparing
  27.         type 1 reports for the rep.c module.  Rep.c uses the special eop
  28.         markers to identify paragraphs in the printing code.
  29.     
  30. _____________________________________________________________________*/
  31.  
  32. #include <stdio.h>
  33. #include    <stdlib.h>
  34. #include <string.h>
  35. #include    <ctype.h>
  36. #include    <console.h>
  37.  
  38. #include "doc.h"
  39.  
  40. short main(short argc, char *argv[])
  41.  
  42. {
  43.     char        line[10000];
  44.     char        *first;
  45.     char        *last;
  46.     char        *prev;
  47.     char        *next;
  48.     char        save;
  49.     short        len;
  50.     GrafPort    myPort;
  51.     Boolean    rSpecified;
  52.     Boolean    pSpecified;
  53.     short        margin;
  54.     short        i;
  55.     
  56.     /* Obtain parameters. */
  57.     argc = ccommand(&argv);
  58.  
  59.     /* Crack and check parameters. */
  60.  
  61.     i = 1;
  62.     rSpecified = pSpecified = false;
  63.     while (i < argc) {
  64.         if (*argv[i] == '-') {
  65.             if (tolower(*(argv[i]+1)) == 'r') {
  66.                 rSpecified = true;
  67.                 margin = atoi(argv[i+1]);
  68.                 i += 2;
  69.             } else if (tolower(*(argv[i]+1)) == 'p') {
  70.                 pSpecified = true;
  71.                 i += 2;
  72.             } else {
  73.                 fprintf(stderr, "### %s - Usage: %s -r rightmargin [-p].\n", 
  74.                     argv[0], argv[0]);
  75.                 return 1;
  76.             };
  77.         } else {
  78.             fprintf(stderr, "### %s - Usage: %s -r rightmargin [-p].\n", 
  79.                 argv[0], argv[0]);
  80.             return 1;
  81.         };
  82.     };
  83.     if (!rSpecified) {
  84.         fprintf(stderr, "### %s - right margin not specified.\n", argv[0]);
  85.         return 1;
  86.     };
  87.  
  88.     /* Process file. */
  89.     
  90.     InitGraf((Ptr)&thePort);
  91.     OpenPort(&myPort);
  92.     TextFont(applFont);
  93.     TextSize(9);
  94.     while (gets(line)) {
  95.         first = last = prev = line;
  96.         len = strlen(line);
  97.         *(line+len) = ' ';
  98.         *(line+len+1) = 0;
  99.         while (true) {
  100.             while (true) {
  101.                 next = strchr(prev, ' ');
  102.                 if (!next || TextWidth(first, 0, next-first) > margin ) break;
  103.                 last = next;
  104.                 prev = last + strspn(last, " ");
  105.             };
  106.             save = *last;
  107.             if (next) {
  108.                 *last = 0;
  109.                 puts(first);
  110.                 *last = save;
  111.                 first = prev;
  112.                 last = next;
  113.             } else {
  114.                 if (pSpecified) {
  115.                     *last = docEop;
  116.                     *(last+1) = 0;
  117.                     puts(first);
  118.                     break;
  119.                 } else {
  120.                     *last = 0;
  121.                     puts(first);
  122.                     break;
  123.                 };
  124.             };
  125.         };
  126.     };
  127.     
  128.     return 0;
  129. }